/*
 * @Author: lwHao
 * @Date: 2020-11-04 15:44:47
 * @LastEditors: lwHao
 * @LastEditTime: 2020-11-08 17:52:35
 */
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`

const axios = require('axios')

module.exports = function (api) {
  api.loadSource(async actions => {
    const postCollection = actions.addCollection('Post')

    const { data } = await axios.get('https://jsonplaceholder.typicode.com/posts')

    for (const item of data) {
      postCollection.addNode(item)
    }

    const userCollection = actions.addCollection('Users')
    const { data: userData } = await axios.get('https://jsonplaceholder.typicode.com/users')
    for (const item of userData) {
      userCollection.addNode(item)
    }
    const projectCollection = actions.addCollection('Project')
    const { data: projectList } = await axios.get('https://gitee.com/api/v5/user/repos?access_token=e91fadbe509d1edfeadecb03f42da00d&sort=full_name&page=1&per_page=20')
    for (const item of projectList) {
      projectCollection.addNode(item)
    }
  })
  api.createPages(({ createPage }) => {
    // Use the Pages API here: https://gridsome.org/docs/pages-api/
    // createPage({
    //   path: '/blog/info/:id',
    //   component: './src/templates/Blog.vue'
    // })
  })
}
